package gw.demo2.service;

import gw.demo2.entity.Admin;
import gw.demo2.entity.Merchant;
import gw.demo2.entity.PurchaseRecord;
import gw.demo2.entity.User;
import gw.demo2.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import gw.demo2.mapper.UserMapper;
import gw.demo2.mapper.MerchantMapper;
import gw.demo2.mapper.PurchaseRecordMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;

    public Admin login(int adminId, String password) {
        Admin admin = adminMapper.selectAdminById(adminId);
        if (admin != null && admin.getPassword().equals(password)) {
            return admin;
        }
        return null;
    }
    public List<User> getAllUsers() {
        return userMapper.selectAllUsers();
    }

    public List<Merchant> getAllMerchants() {
        return merchantMapper.selectAllMerchants();
    }
    public Map<String, Integer> getCounts() {
        Map<String, Integer> counts = new HashMap<>();

        counts.put("userCount", userMapper.selectUsersCount());
        counts.put("merchantCount", merchantMapper.selectMerchantsCount());
        return counts;
    }

    public List<PurchaseRecord> getPurchaseRecords() {
        return purchaseRecordMapper.selectAllPurchaseRecords();
    }

    public boolean deleteUser(int userId) {
        return userMapper.deleteUser(userId) > 0;
    }

    public boolean deleteMerchant(int merchantId) {
        return merchantMapper.deleteMerchant(merchantId) > 0;
    }

    public boolean updateAdminPassword(int adminId, String newPassword) {
        Admin admin = new Admin();
        admin.setAdminId(adminId);
        admin.setPassword(newPassword);
        return adminMapper.updateAdmin(admin) > 0;
    }
}